package com.odm.communitymanage.ui.idle

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.DiffUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.core.bean.IdleData
import com.odm.communitymanage.util.GlideUtils


/**
 * @description: 闲置列表适配器
 * @author: ODM
 * @date: 2020/4/17
 */
class IdleAdapter (data : MutableList<IdleData>)
    : BaseQuickAdapter<IdleData, BaseViewHolder>(R.layout.item_idle_summary, data) {

    override fun convert(holder: BaseViewHolder, item: IdleData) {
        holder.setText(R.id.tvTitle_IdleSummary , item.topic)
        holder.setText(R.id.tvPrice_IdleSummary ,"￥"+item.price.toString())
        val iv : ImageView = holder.getView<ImageView>(R.id.ivPhoto_IdleSummary)
        iv.visibility = View.VISIBLE
        item.imgs?.let {
            if(item.imgs.isNotEmpty()){
                //加载第一张图片
                val options = RequestOptions().override(300 , 400)
                                            //同时缓存原图和结果图
                                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                                            //跳过内存缓存
                                            .skipMemoryCache(true)
                GlideUtils
                    .getInstance()
                    .loadImageUrl(iv ,options,item.imgs[0] , iv)
            }
        }
    }

     class IdleDiffCallback(private val oldList: List<IdleData> ?, private val newList: List<IdleData> ?) :
        DiffUtil.Callback() {

        override fun getOldListSize(): Int {
            return oldList?.size ?: 0
        }

        override fun getNewListSize(): Int {
            return newList?.size ?: 0
        }

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldList?.get(oldItemPosition)?.javaClass == newList?.get(newItemPosition)?.javaClass
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return  oldList?.get(oldItemPosition)?.id == newList?.get(newItemPosition)?.id &&
                    oldList?.get(oldItemPosition)?.imgs == newList?.get(newItemPosition)?.imgs
        }
    }

}